﻿using System;

namespace Trentacular.SharePoint.StsAdmin
{
    public class BaseStsadmHelper : BaseConsoleHelper
    {
        private JobDefinitionExecutor _jobDefinitionExecuter;
        public JobDefinitionExecutor JobDefinitionExecuter
        {
            get
            {
                if (_jobDefinitionExecuter == null)
                    _jobDefinitionExecuter = new JobDefinitionExecutor();
                return _jobDefinitionExecuter;
            }
            set { _jobDefinitionExecuter = value; }
        }

        protected void ExecuteJobsAndCleanupFailures()
        {
            var report = JobDefinitionExecuter.ExecuteJobDefinitions();
            if (report.HasFailedJobs)
            {
                foreach (var job in report.FailedJobs)
                {
                    DoConsoleAction(string.Format("Deleting failed job '{0}'", job.Title), delegate()
                    {
                        job.Delete();
                    });
                }
            }
        }
    }
}
